package com.tstudy.laoshibang.custom;

import com.tstudy.laoshibang.R;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.widget.RelativeLayout;


@SuppressLint({ "Recycle", "DrawAllocation" })
public class BorderRelativeLayout extends RelativeLayout {
	boolean mBorderLeft;
	boolean mBorderTop;
	boolean mBorderRight;
	boolean mBorderBottom;
	int mBorderColor;
	int mBorderWidth;
	

	public BorderRelativeLayout(Context context) {
		super(context);
	}
	

	public BorderRelativeLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
		 TypedArray typedArray = context.obtainStyledAttributes(attrs,R.styleable.BorderTextView);
		 mBorderColor = typedArray.getColor(R.styleable.BorderTextView_borderColor, 0);
		 mBorderLeft = typedArray.getBoolean(R.styleable.BorderTextView_borderLeft,true);
		 mBorderTop = typedArray.getBoolean(R.styleable.BorderTextView_borderTop, true);
		 mBorderRight = typedArray.getBoolean(R.styleable.BorderTextView_borderRight,true);
		 mBorderBottom = typedArray.getBoolean(R.styleable.BorderTextView_borderBottom,true);
		 mBorderWidth = (int) typedArray.getDimension(R.styleable.BorderTextView_borderWidth, 0);
	}
	
	public void setBorder(boolean borderLeft,boolean borderTop,boolean borderRight,boolean borderBottom){
		mBorderLeft = borderLeft;
		mBorderTop = borderTop;
		mBorderRight = borderRight;
		mBorderBottom = borderBottom;
	}
	
	public void setBorderWidth(int borderWidth){
		mBorderWidth = borderWidth;
	}
	
	public void setBorderColor(int borderColor){
		mBorderColor = borderColor;
	}

	public BorderRelativeLayout(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		
	}

	@Override
	protected void dispatchDraw(Canvas canvas){
		super.dispatchDraw(canvas);
		Paint paint = new Paint();
		paint.setColor(mBorderColor);
		// 画TextView的4个边
//		if(mBorderLeft){
//			canvas.drawLine(0, 0, 0, this.getHeight() - mBorderWidth, paint);//左边
//		}
//		if(mBorderTop){
//			canvas.drawLine(0, 0, this.getWidth() - mBorderWidth, 0, paint);//上边
//		}
//		if(mBorderRight){
//			canvas.drawLine(this.getWidth() - mBorderWidth, 0, this.getWidth() - mBorderWidth,this.getHeight() - mBorderWidth, paint);//右边
//		}
//		if(mBorderBottom){
//			canvas.drawLine(0, this.getHeight() - mBorderWidth, this.getWidth() - mBorderWidth,this.getHeight() - mBorderWidth, paint);//下边
//			
//		}
//		mBorderWidth = CommonUtil.dip2px(mBorderWidth);
		if(mBorderLeft){
			canvas.drawRect(0, 0, mBorderWidth, this.getHeight(), paint);//左边
		}
		if(mBorderTop){
			canvas.drawRect(0, 0, this.getWidth(), mBorderWidth, paint);//上边
		}
		if(mBorderRight){
			canvas.drawRect(this.getWidth() - mBorderWidth, 0, this.getWidth(),this.getHeight(), paint);//右边
		}
		if(mBorderBottom){
			canvas.drawRect(0, this.getHeight() - mBorderWidth, this.getWidth(),this.getHeight(), paint);//下边
			
		}
	}

}
